package org.loofer.ext.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;
import android.os.Process;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public final class ProcessUtils {
    private static final int[] EMPTY_INT_ARRAY = new int[0];
    private static final Object sMainLock = new Object();
    private static volatile Boolean sMainProcess;
    private static final Object sNameLock = new Object();
    private static volatile String sProcessName;

    private ProcessUtils() {
    }

    public static String myProcessName(Context context) {
        if (sProcessName != null) {
            return sProcessName;
        }
        synchronized (sNameLock) {
            if (sProcessName != null) {
                String str = sProcessName;
                return str;
            }
            str = obtainProcessName(context);
            sProcessName = str;
            return str;
        }
    }

    public static boolean isMainProcess(Context context) {
        if (sMainProcess != null) {
            return sMainProcess.booleanValue();
        }
        synchronized (sMainLock) {
            if (sMainProcess != null) {
                boolean booleanValue = sMainProcess.booleanValue();
                return booleanValue;
            }
            String processName = myProcessName(context);
            if (processName == null) {
                return false;
            }
            sMainProcess = Boolean.valueOf(processName.equals(context.getApplicationInfo().processName));
            booleanValue = sMainProcess.booleanValue();
            return booleanValue;
        }
    }

    public void killSelf() {
        Process.killProcess(Process.myPid());
    }

    public static void killAll(Context context) {
        killAll(context, EMPTY_INT_ARRAY);
    }

    public static void killAll(Context context, int... excludePid) {
        int myPid = Process.myPid();
        Collection<RunningAppProcessInfo> runningProcess = collectRunningProcessInfo(context);
        Set<Integer> excludePidSet = collectUniqueSet(excludePid);
        if (runningProcess != null) {
            for (RunningAppProcessInfo process : runningProcess) {
                if ((excludePidSet == null || !excludePidSet.contains(Integer.valueOf(process.pid))) && myPid != process.pid) {
                    Process.killProcess(process.pid);
                }
            }
        }
        if (excludePidSet == null || !excludePidSet.contains(Integer.valueOf(myPid))) {
            Process.killProcess(myPid);
        }
    }

    public static void killAll(Context context, String... excludeName) {
        int myPid = Process.myPid();
        String myProcessName = null;
        Collection<RunningAppProcessInfo> runningProcess = collectRunningProcessInfo(context);
        Set<String> excludeNameSet = collectUniqueSet((Object[]) excludeName);
        if (runningProcess != null) {
            for (RunningAppProcessInfo process : runningProcess) {
                if (excludeNameSet == null || !excludeNameSet.contains(process.processName)) {
                    if (myPid == process.pid) {
                        myProcessName = process.processName;
                    } else {
                        Process.killProcess(process.pid);
                    }
                }
            }
        }
        if (myProcessName == null || excludeNameSet == null || !excludeNameSet.contains(myProcessName)) {
            Process.killProcess(myPid);
        }
    }

    public static boolean isForeground(Context context) {
        List<RunningTaskInfo> runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
        if (runningTasks == null || runningTasks.isEmpty()) {
            return false;
        }
        RunningTaskInfo topTask = (RunningTaskInfo) runningTasks.get(0);
        if (topTask == null) {
            return false;
        }
        String myPackageName = context.getPackageName();
        if (myPackageName.equals(topTask.baseActivity.getPackageName()) || myPackageName.equals(topTask.topActivity.getPackageName())) {
            return true;
        }
        return false;
    }

    private static Collection<RunningAppProcessInfo> collectRunningProcessInfo(Context context) {
        Collection<RunningAppProcessInfo> runningProcesses = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses();
        if (runningProcesses != null) {
            int uid = Process.myUid();
            Iterator<RunningAppProcessInfo> iterator = runningProcesses.iterator();
            while (iterator.hasNext()) {
                RunningAppProcessInfo process = (RunningAppProcessInfo) iterator.next();
                if (process == null || process.uid != uid) {
                    iterator.remove();
                }
            }
        }
        return runningProcesses;
    }

    private static <V> Set<V> collectUniqueSet(V... values) {
        if (values == null || values.length == 0) {
            return null;
        }
        Set<V> set = new HashSet(values.length);
        for (V value : values) {
            set.add(value);
        }
        return set;
    }

    private static Set<Integer> collectUniqueSet(int... values) {
        if (values == null || values.length == 0) {
            return null;
        }
        Set<Integer> set = new HashSet(values.length);
        for (int value : values) {
            set.add(Integer.valueOf(value));
        }
        return set;
    }

    private static String obtainProcessName(Context context) {
        int pid = Process.myPid();
        List<RunningAppProcessInfo> listTaskInfo = ((ActivityManager) context.getSystemService("activity")).getRunningAppProcesses();
        if (listTaskInfo != null && listTaskInfo.size() > 0) {
            for (RunningAppProcessInfo proc : listTaskInfo) {
                if (proc != null && proc.pid == pid) {
                    return proc.processName;
                }
            }
        }
        return null;
    }
}
